package cn.jiyun.manage.product.service.impl;

import cn.jiyun.common.vo.CommonResult;
import cn.jiyun.manage.product.feign.ProductCoupon;
import cn.jiyun.manage.product.feign.ProductFenilei;
import cn.jiyun.manage.product.feign.ProductGuigeFeign;
import cn.jiyun.manage.product.feign.ProductPinglun;
import cn.jiyun.manage.product.mapper.*;
import cn.jiyun.manage.product.service.ManageService;
import cn.jiyun.model.*;
import cn.jiyun.model.dto.ManageDto;
import cn.jiyun.model.dto.SettingsDto;
import cn.jiyun.model.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private ProductPinglun productPinglun;
    @Autowired
    private ProductCoupon productCoupon;
    @Autowired
    private PrductBaseMapper prductBaseMapper;
    @Autowired
    private LogisticsMapper logisticsMapper;
    @Autowired
    private DetailMapper detailMapper;
    @Autowired
    private SpecsMapper specsMapper;
    @Autowired
    private OtherMapper otherMapper;
    @Autowired
    private SettingsMapper settingsMapper;
    @Autowired
    private  SettingsGoldMapper settingsGoldMapper;
    @Autowired
    private  SettingsVipMapper settingsVipMapper;
    @Autowired
    private ProductFenilei productFenilei;
   @Autowired
   private ProductGuigeFeign productGuigeFeign;
    @Override
    public CommonResult findAdd(ManageDto manageDto) {
        Base base = manageDto.getBase();
        prductBaseMapper.findAdd(base);
        Logistics logistics = manageDto.getLogistics();
        logistics.setLogisticsBaseId(base.getBaseId());
        logisticsMapper.insert(logistics);
        Detail detail = manageDto.getDetail();
        detail.setDetailBaseId(base.getBaseId());
        detailMapper.insert(detail);
        Spercs spercs = manageDto.getSpercs();
        spercs.setSpercsBaseId(base.getBaseId());
        specsMapper.insert(spercs);
        Other other = manageDto.getOther();
        other.setOtherBaseId(base.getBaseId());
        otherMapper.insert(other);
        Settings settings = manageDto.getSettings();
        settings.setSettingsBaseId(base.getBaseId());
        settingsMapper.insert(settings);
        SettingsDto settingsDto = manageDto.getSettingsdto();
        if (settings.getSettingsAloneSettings()!=null){
            String[] split = settings.getSettingsAloneSettings().split(",");
            for (String s : split) {
                if (s.equals("1")){
                    SettingsVip settingsVip = new SettingsVip();
                    BeanUtils.copyProperties(settingsDto,settingsVip);
                    settingsVipMapper.insert(settingsVip);
                }else if (s.equals("0")){
                    SettingsGold settingsGold = new SettingsGold();
                    BeanUtils.copyProperties(settingsDto,settingsGold);
                   settingsGoldMapper.insert(settingsGold);
                }
            }
        }
        return CommonResult.success();
    }

    @Override
    public  List<ManageListVo> findAll() {
        List<Base> bases = prductBaseMapper.selectList(null);
        List<ManageListVo> manageListVos = new ArrayList<>();
        bases.stream().forEach(base -> {
            LambdaQueryWrapper<Spercs> spercsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            spercsLambdaQueryWrapper.eq(Spercs::getSpercsBaseId,base.getBaseId());
            Spercs spercs2 = specsMapper.selectOne(spercsLambdaQueryWrapper);
            LambdaQueryWrapper<Settings> spercsLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            spercsLambdaQueryWrapper1.eq(Settings::getSettingsBaseId,base.getBaseId());
            Settings settings1 = settingsMapper.selectOne(spercsLambdaQueryWrapper1);
            ManageListVo manageListVo = new ManageListVo();
            if(spercs2!=null){
                BeanUtils.copyProperties(spercs2,manageListVo);
            }
           if (settings1!=null){
               BeanUtils.copyProperties(settings1,manageListVo);
           }

            BeanUtils.copyProperties(base,manageListVo);
            manageListVo.setCollect(1000);
            manageListVo.setSales(3);
            Integer id = base.getBaseFenleiId();
            if (id!=null){
                String fenleiname = productFenilei.fenleiname(id);
                manageListVo.setBaseFenleiname(fenleiname);
            }


            manageListVos.add(manageListVo);
        });
        return manageListVos;
    }

    @Override
    public Base manageFeign(Integer id) {
        return prductBaseMapper.selectById(id);
    }

    @Override
    public List<ManageBaseSpercsVo> findAllmanage() {
        List<ManageBaseSpercsVo> manageBaseSpercsVos = new ArrayList<>();
        List<Base> bases = prductBaseMapper.selectList(null);
        List<Spercs> spercs = specsMapper.selectList(null);
        BeanUtils.copyProperties(bases,manageBaseSpercsVos);
        BeanUtils.copyProperties(spercs,manageBaseSpercsVos);
        for (Base basis : bases) {
            ManageBaseSpercsVo manageBaseSpercsVo = new ManageBaseSpercsVo();
            List<Spercs> collect = spercs.stream().filter(spercs1 -> spercs1.getSpercsBaseId().equals(basis.getBaseId())).collect(Collectors.toList());
            for (Spercs spercs1 : collect) {
                BeanUtils.copyProperties(spercs1,manageBaseSpercsVo);
                List<ProductGuige> guigefeign1 = productGuigeFeign.findGuigefeign1();
                if (guigefeign1!=null){
                    for (ProductGuige productGuige : guigefeign1) {
                        if (spercs1.getSpercsGuigeId()!=null){
                            if (spercs1.getSpercsGuigeId().equals(productGuige.getId())){
                                manageBaseSpercsVo.setSpercsGuigename(productGuige.getName());
                            }
                        }

                    }
                }

            }
            BeanUtils.copyProperties(basis,manageBaseSpercsVo);
            manageBaseSpercsVos.add(manageBaseSpercsVo);
        }
        return manageBaseSpercsVos;
    }

    @Override
    public Spercs specsFeign(Integer id) {
        Spercs spercs = specsMapper.selectById(id);
        return spercs;
    }

    @Override
    public List<BaseOtherSpecsVo> baseFeign() {
        List<Base> bases = prductBaseMapper.selectList(null);
        List<Other> others = otherMapper.selectList(null);
        List<Detail> details = detailMapper.selectList(null);
        List<Settings> settings = settingsMapper.selectList(null);
        List<BaseOtherSpecsVo> baseOtherSpecsVos = new ArrayList<>();
        bases.stream().forEach(base -> {
            BaseOtherSpecsVo baseOtherSpecsVo = new BaseOtherSpecsVo();
            List<Other> collect = others.stream().filter(other -> other.getOtherBaseId().equals(base.getBaseId())).collect(Collectors.toList());
            List<Detail> collect2 = details.stream().filter(detail -> detail.getDetailBaseId().equals(base.getBaseId())).collect(Collectors.toList());
            List<Settings> collect3 = settings.stream().filter(settings1 -> settings1.getSettingsBaseId().equals(base.getBaseId())).collect(Collectors.toList());
          if (collect.size()!=0){
              baseOtherSpecsVo.setOtherProfile(collect.get(0).getOtherProfile());
          }
        if (collect2.size()!=0){
            baseOtherSpecsVo.setDetailDesc(collect2.get(0).getDetailDesc());
        }
        if (collect3.size()!=0){
            baseOtherSpecsVo.setSettingsSort(collect3.get(0).getSettingsSort());
        }
        if (base!=null){
            baseOtherSpecsVo.setBaseName(base.getBaseName());
            baseOtherSpecsVo.setBaseImageUrl(base.getBaseImageUrl());
            baseOtherSpecsVo.setBaseUnit(base.getBaseUnit());
            baseOtherSpecsVo.setBaseId(base.getBaseId());
        }

            baseOtherSpecsVos.add(baseOtherSpecsVo);
        });
        return baseOtherSpecsVos;
    }

    @Override
    public List<Base> findByidBase(Integer id) {
//        skip() 和 take()
        LambdaQueryWrapper<Base> baseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseLambdaQueryWrapper.eq(Base::getBaseFenleiId,id);
        baseLambdaQueryWrapper.orderByDesc(Base::getBaseTime);
        List<Base> bases1 = new ArrayList<>();
        List<Base> bases = prductBaseMapper.selectList(baseLambdaQueryWrapper);
        if (bases.size()>10){
            for (int i = 0; i <10; i++) {
                bases1.add(bases.get(i));
            }
            return bases1;
        }
        return bases;
    }

    @Override
    public List<SellersVo> commTui(Integer id) {
        List<Settings> settings = settingsMapper.selectList(null);
        List<SellersVo> sellersVos = new ArrayList<>();
        settings.stream().forEach(settings1 -> {
            if (settings1.getSettingsCommTui()!=null){
            String[] split = settings1.getSettingsCommTui().split(",");
                for (String s : split) {
                    if (s.equals(id + "")) {
                        SellersVo sellersVo = new SellersVo();
                        sellersVo.setBaseId(settings1.getSettingsBaseId());
                        sellersVo.setSettingsSoldNum(settings1.getSettingsSoldNum());
                        Base base = prductBaseMapper.selectById(settings1.getSettingsBaseId());
                        if (base!=null){
                            sellersVo.setBaseName(base.getBaseName());
                            sellersVo.setBaseImageUrl(base.getBaseImageUrl());
                        }
                        LambdaQueryWrapper<Spercs> spercsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        spercsLambdaQueryWrapper.eq(Spercs::getSpercsBaseId, settings1.getSettingsBaseId());
                        Spercs spercs = specsMapper.selectOne(spercsLambdaQueryWrapper);
                        if (spercs!=null){
                            sellersVo.setSpercsPrice(spercs.getSpercsPrice());
                        }

                        sellersVos.add(sellersVo);
                    }
                }
            }
        });

        return sellersVos;
    }

    @Override
    public Spercs specsbaseFeign(Integer baseid) {
        LambdaQueryWrapper<Spercs> spercsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spercsLambdaQueryWrapper.eq(Spercs::getSpercsBaseId,baseid);
        Spercs spercs = specsMapper.selectOne(spercsLambdaQueryWrapper);
        return spercs;
    }

    @Override
    public CommonResult productInformation(Integer id) {
        ManagePingLunGuige managePingLunGuige = new ManagePingLunGuige();
        Base base = prductBaseMapper.selectById(id);
        if (base!=null){
            managePingLunGuige.setBaseName(base.getBaseName());
            managePingLunGuige.setBaseImageUrl(base.getBaseImageUrl());
        }
        LambdaQueryWrapper<Spercs> spercsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spercsLambdaQueryWrapper.eq(Spercs::getSpercsBaseId,id);
            Spercs spercs = specsMapper.selectOne(spercsLambdaQueryWrapper);
            if (spercs!=null){
                managePingLunGuige.setSpecsStock(spercs.getSpercsStock());
                managePingLunGuige.setSpecsPrice(spercs.getSpercsPrice());
                managePingLunGuige.setSpecsYuanprice(spercs.getSpercsYuanprice());
            }
            List<ProductPingLun> baseByid = productPinglun.findBaseByid(id);
            if (baseByid!=null){
                managePingLunGuige.setProductPingLun(baseByid);
            }
            List<ProductGuige> beanByid1 = productGuigeFeign.findBeanByid(id);
            if (beanByid1!=null){
                managePingLunGuige.setProductGuige(beanByid1);
            }
        return CommonResult.success(managePingLunGuige) ;
    }

    @Override
    public CommonResult productInformationXiangQing(Integer id) {
        ManagePingLunGuigeCoupon managePingLunGuige = new ManagePingLunGuigeCoupon();
        Base base = prductBaseMapper.selectById(id);
        if (base!=null){
            managePingLunGuige.setBaseName(base.getBaseName());
            managePingLunGuige.setBaseImageUrl(base.getBaseImageUrl());
        }
        LambdaQueryWrapper<Detail> detailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        detailLambdaQueryWrapper.eq(Detail::getDetailBaseId,id);
        Detail detail = detailMapper.selectOne(detailLambdaQueryWrapper);
        if (detail!=null){
            managePingLunGuige.setDetailDesc(detail.getDetailDesc());
        }
        LambdaQueryWrapper<Spercs> spercsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spercsLambdaQueryWrapper.eq(Spercs::getSpercsBaseId,id);
        Spercs spercs = specsMapper.selectOne(spercsLambdaQueryWrapper);
        if (spercs!=null){
            managePingLunGuige.setSpecsStock(spercs.getSpercsStock());
            managePingLunGuige.setSpecsPrice(spercs.getSpercsPrice());
            managePingLunGuige.setSpecsYuanprice(spercs.getSpercsYuanprice());
        }
        List<ProductPingLun> baseByid = productPinglun.findBaseByid(id);
        if (baseByid!=null){
            managePingLunGuige.setProductPingLun(baseByid);
        }
        List<ProductGuige> beanByid1 = productGuigeFeign.findBeanByid(id);
        if (beanByid1!=null){
            managePingLunGuige.setProductGuige(beanByid1);
        }
        CouponList couponList = productCoupon.couponList(base);
        managePingLunGuige.setProductCoupon(couponList);
        return CommonResult.success(managePingLunGuige) ;
    }
}
